package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.soda.bean.Fmenu;
import com.soda.bean.Role;
import com.soda.service.FmenuService;
import com.soda.service.RoleService;
import com.soda.service.impl.FmenuServiceImpl;
import com.soda.service.impl.RoleServiceImpl;
import com.soda.util.PageInfo;
import com.soda.util.SelectRoleInfo;

public class RoleServlet extends HttpServlet {

	RoleService rs = new RoleServiceImpl();
	FmenuService fs = new FmenuServiceImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		String param = request.getParameter("param");
		if (param != null) {
			if ("selectAllRole".equals(param)) {
				selectAllRole(request, response);
			}
			if ("showAllRole".equals(param)) {
				if (request.getParameter("aaa") != null) {
					request.getSession().removeAttribute("sri");
				}
				showAllRole(request, response);
			}
			if ("showRoleByID".equals(param)) {

				showRoleByID(request, response);

			}
			if ("editRole".equals(param)) {
				editRole(request, response);
			}
			if ("saveRoleInfo".equals(param)) {
				saveRoleInfo(request, response);
			}
			if ("editState".equals(param)) {
				editState(request, response);
			}
			if ("deleteById".equals(param)) {
				deleteById(request, response);
			}
			if ("addRole".equals(param)) {
				addRole(request, response);
			}
			if ("addRoleInfo".equals(param)) {
				addRoleInfo(request, response);
			}
			if ("ajaxrolename".equals(param)) {
				ajaxrolename(request, response);
			}
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	// 查询所有角色个user
	public void selectAllRole(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Role> roles = rs.getAllRole();
		request.setAttribute("roles", roles);
		request.getRequestDispatcher("/houtai/pages/user/add.jsp").forward(
				request, response);

	}

	// 查询所有角色 //分页
	public void showAllRole(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		PageInfo pi = new PageInfo();
		SelectRoleInfo sri = new SelectRoleInfo();

		String rolName = request.getParameter("selrolName");
		String rolState = request.getParameter("selrolState");

		if (rolName != null) {
			sri.setName(rolName.length() == 0 ? null : rolName);
			sri.setState(rolState.length() == 0 ? null : Integer
					.parseInt(rolState));
			request.getSession().setAttribute("sri", sri);
		} else {
			sri = (SelectRoleInfo) request.getSession().getAttribute("sri");
		}

		// 分页信息
		pi.setPer(10);
		pi.setTotal(rs.getAllCount(sri));

		String page = request.getParameter("page");
		if (page != null) {
			pi.setPage(Integer.parseInt(page));
		}

		// 根据条件获得集合
		List<Role> roles = rs.getAllRoleBySelectInfo(sri, pi);

		// 保存数据
		request.getSession().setAttribute("sri", sri);
		request.setAttribute("pi", pi);
		request.setAttribute("roles", roles);
		request.getRequestDispatcher("/houtai/pages/role/list.jsp").forward(
				request, response);
	}

	// 通过id查询角色
	public void showRoleByID(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		// 根据id查询该role
		// 查询所有父菜单 及 子级菜单
		// 查询该角色拥有的父菜单及子级菜单
		// 在页面判断如果id相等则checked
		String rolId = request.getParameter("id");
		// 1
		Role role = rs.getRoleById(Integer.parseInt(rolId));

		// 2查询父级菜单，同时把其所拥有的子菜单放进父菜单

		List<Fmenu> fmenus = fs.getAllFmenu();

		// 3根据角色查询对应父级菜单及子级菜单

		request.setAttribute("role", role);// 该角色及角色对应的父级菜单和子级菜单
		request.setAttribute("fmenus", fmenus);// 所有的父菜单和子菜单

		request.getRequestDispatcher("/houtai/pages/role/detail.jsp").forward(
				request, response);
	}

	// 通过id查询角色
	public void editRole(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		// 根据id查询该role
		// 查询所有父菜单 及 子级菜单
		// 查询该角色拥有的父菜单及子级菜单
		// 在页面判断如果id相等则checked
		String rolId = request.getParameter("id");
		// 1
		Role role = rs.getRoleById(Integer.parseInt(rolId));

		// 2查询父级菜单，同时把其所拥有的子菜单放进父菜单

		List<Fmenu> fmenus = fs.getAllFmenu();

		// 3根据角色查询对应父级菜单及子级菜单

		request.setAttribute("role", role);// 该角色及角色对应的父级菜单和子级菜单
		request.setAttribute("fmenus", fmenus);// 所有的父菜单和子菜单
		request.getRequestDispatcher("/houtai/pages/role/edit.jsp").forward(
				request, response);
	}

	public void saveRoleInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		int rolId = Integer.parseInt(request.getParameter("rolid"));
		int rolState = Integer.parseInt(request.getParameter("rolState"));
		String rolRemark = request.getParameter("rolRemark");
		String rolName = request.getParameter("rolName");

		// 查询父菜单所有id

		List<Integer> fids = fs.getAllFmenuId();

		// 先清空role关联的所有父菜单及子菜单的关系
		rs.deleteRoleToFmenuAndMenu(rolId);
		// 然后把父级标签自己标签增加到数据库
		for (int i = 0; i < fids.size(); i++) {
			String[] ss = request.getParameterValues("fu" + fids.get(i));

			if (ss != null) {
				// name值相等的参数，数组的第一个为父菜单值 其余为子菜单值
				if (rs.editRoleToFmenu(rolId, Integer.parseInt(ss[0])) > 0) {// 大于0，添加成功

					for (int j = 1; j < ss.length; j++) {
						rs.addRoleToMenu(rolId, Integer.parseInt(ss[j]));
					}
				}
			}
		}
		// 修改成功
		if (rs.editRoleInfo(rolId, rolName, rolState, rolRemark) > 0) {
			// 更新信息
			// 如果成功则跳转
			showAllRole(request, response);
		}
	}

	public void editState(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {

		request.setCharacterEncoding("utf-8");

		int id = Integer.parseInt(request.getParameter("id"));
		int state = Integer.parseInt(request.getParameter("state"));
		// 异步修改成功
		rs.editState(id, state);
	}

	public void deleteById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		int id = Integer.parseInt(request.getParameter("id"));

		// 假删，即改变delete状态
		if (rs.deleteById(id) > 0) {
			// 删除成功
			showAllRole(request, response);
		}

	}

	public void addRole(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Fmenu> fmenus = rs.getAllFmenuAndMenu();

		request.setAttribute("fmenus", fmenus);
		request.getRequestDispatcher("houtai/pages/role/add.jsp").forward(
				request, response);

	}

	public void addRoleInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		int rolState = Integer.parseInt(request.getParameter("rolState"));
		String rolRemark = request.getParameter("rolRemark");
		String rolName = request.getParameter("rolName");

		// 查询父菜单所有id

		List<Integer> fids = fs.getAllFmenuId();

		// 先把role增加到数据库，并查询出新增的id
		int rolId = rs.addRoleInfo(rolName, rolState, rolRemark);
		if (rolId == -1)
			return;
		// 然后把父级标签自己标签增加到数据库
		for (int i = 0; i < fids.size(); i++) {
			String[] ss = request.getParameterValues("fu" + fids.get(i));

			if (ss != null) {
				// name值相等的参数，数组的第一个为父菜单值 其余为子菜单值
				if (rs.editRoleToFmenu(rolId, Integer.parseInt(ss[0])) > 0) {// 大于0，添加成功

					for (int j = 1; j < ss.length; j++) {
						rs.addRoleToMenu(rolId, Integer.parseInt(ss[j]));
					}
				}
			}
		}
		// 修改成功
		// 更新信息
		// 如果成功则跳转
		showAllRole(request, response);
	}

	public void ajaxrolename(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String rolname = new String(request.getParameter("rolname").getBytes(
				"ISO-8859-1"), "utf-8");
		String ids = request.getParameter("id");
		if (ids != null) {
			int id = Integer.parseInt(ids);
			String rname = rs.getRoleNameById(id);
			if (rname.equals(rolname)) {
				return;
			} else {
				if (rs.ajaxrolename(rolname) > 0) {
					out.print("角色名已存在");
				} else {
					out.print("√");
				}
			}
		}else{
			if (rs.ajaxrolename(rolname) > 0) {
				out.print("角色名已存在");
			} else {
				out.print("√");
			}
		}

	}
}
